package com.ruitu.health_2.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.BookModel;
import com.ruitu.health_2.mvp.presenter.BookPresenter;
import com.ruitu.health_2.ui.activity.BookDetailActivity;
import com.ruitu.router_module.bean.product.Book;
import com.ruitu.router_module.util.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ReadPartyBookFragment extends BaseFragment<BookPresenter.ReadPartyBookListP, BookModel.BookListM> {
    private RecyclerView rcv_list;
    private RelativeLayout rl_empty;

    private List<Book> bookList = new ArrayList<>();
    private ReadPartyBookListAdpter adapter;
    public static String readPartyId;
    public String type;

    public static ReadPartyBookFragment newInstance(String readPartyId, String type) {
        ReadPartyBookFragment.readPartyId = readPartyId;

        Bundle args = new Bundle();
        ReadPartyBookFragment fragment = new ReadPartyBookFragment();
        args.putString("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        type = getArguments().getString("type");
        adapter = new ReadPartyBookListAdpter(getActivity());
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("name", bookList.get(position).getBookName());
                bundle.putString("content", bookList.get(position).getContentmb());
                startActivity(BookDetailActivity.class, bundle);
            }
        });
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_readparty_book, container, false);
        rcv_list = v.findViewById(R.id.rcv_list);
        rl_empty = v.findViewById(R.id.rl_empty);

        ((TextView) (rl_empty.findViewById(R.id.txt_description))).setText("暂时没有找到相关书籍");

        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        p.reqReadPartyBookList(readPartyId, type);

        return v;
    }

    protected void showOrHideEmptyView() {
        if (isDataEmpty(bookList)) {
            rl_empty.setVisibility(View.VISIBLE);
        } else {
            rl_empty.setVisibility(View.GONE);
        }
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {
            bookList = (List<Book>) data;
            adapter.setDataList(bookList);

            showOrHideEmptyView();
        }
    }

    private class ReadPartyBookListAdpter extends ListBaseAdapter<Book> {
        public ReadPartyBookListAdpter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_readparty_book_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RoundedImageView icon = holder.getView(R.id.icon);
            TextView tv_title = holder.getView(R.id.tv_title);
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_subtitle = holder.getView(R.id.tv_subtitle);

            Book book = mDataList.get(position);
            tv_title.setText(book.getBookName());
            tv_name.setText(book.getArtManName());

            GlideUtil.loadImg(book.getBookCover(), icon);
        }
    }
}
